<?php
namespace App\Http\Controllers\Login;
use App\Http\Controllers\ApiBaseController;
use Illuminate\Http\Request;
use App\Models\AdminModels\Admin;
use App\Models\AdminModels\AdminService;
use App\Models\AdminModels\AdminRandomUser;
use DB;
class AdminServiceAjaxController extends ApiBaseController{
  
  public function __construct(Request $request) {
    parent::__construct($request);
  }
  
  /**
   * 管理员列表
   * ajax数据
   */
  public function dataList(){
    $oAdmin = new Admin();
    $result = $oAdmin->getAdminServiceData($this->params);
    echo json_encode($result);
  }
  
  /**
   * 我的客户关联列表
   */
  public function relationList(){
    $oAdminService = new AdminService();
    $result = $oAdminService->getPageData($this->params);
    echo json_encode($result);
  }

  /**
   * 随机客户列表
   */
  public function randomList(){
    $oAdminRandomUser = new AdminRandomUser();
    $result = $oAdminRandomUser->getRandomPageData($this->params);
    echo json_encode($result);
  }

  /**
   * 客户设置中已关联列表
   */
  public function relationUser(){
    $oAdminService = new AdminService();
    $result = $oAdminService->relationUserData($this->params);
    echo json_encode($result);
  }

  /**
  * 保存关联数据
  */
  public function relationSave(){
    if(isset($this->params['id']) && intval($this->params['id']) > 0){
      if(isset($this->params['ids']) && !empty($this->params['ids'])){
        $relation_ids = explode(',',$this->params['ids']);
        //查询用户是否是客服
        $oAdmin = new Admin();        
        $oAdminService = new AdminService();
        $adminData =$oAdmin->db_get(['admin_id'=>['=',$this->params['id']]]);
        $type = $adminData['type'];//如果不是销售，默认其他都是客服
        foreach ($relation_ids as $relation_id){
            $relationInfo = $oAdminService->db_get(['user_id'=>['=',$relation_id],'type'=>['=',$type]]);
            if(!$relationInfo){
                $data = [];
                $data['admin_id'] = $this->params['id'];
                $data['user_id'] = $relation_id;
                $data['type'] = $type;
                $data['create_time'] = date('Y-m-d H:i:s');
                $oAdminService->db_insert($data);
            }
        }
      }
      $this->msg = '保存成功';
    }else{
      $this->code = 1;
      $this->msg = '缺少数据';
    }
    $this->boot();
  }
  
  /**
   * 关联删除操作
   */
  public function relationDelete(){
    if(isset($this->params['id']) && intval($this->params['id']) > 0){
      $oAdminService = new AdminService();
      $result = $oAdminService->db_get(['id'=>['=',$this->params['id']]]);
      if(!empty($result)){
        if($oAdminService->db_delete('',$this->params['id'])){
          $this->msg = '保存成功';
        }else{
          $this->code = 1;
          $this->msg = '保存失败';
        }
      }else{
        $this->code = 1;
        $this->msg = '数据不存在';
      }
    }else{
      $this->code = 1;
      $this->msg = '缺少数据';
    }
    $this->boot();
  }

  /**
   * 管理员设置为我的客户
   */
  public function adminSetMyService(Request $request){
    if(isset($this->params['user_id']) && intval($this->params['user_id']) > 0){
      $session = $request->session()->get('user');
      $admin_id = $session[0]['admin_id'];
      if($admin_id){
        $oAdmin = new \App\Models\AdminModels\Admin();
        $oAdminService = new \App\Models\AdminModels\AdminService();
        $adminData = $oAdmin->db_get(['admin_id'=>['=',$admin_id],'status'=>['>=',1]]);
        if(!empty($adminData)){
          //查询已经设置的数量
          $todaySetNum = $oAdminService->todayBindNum($admin_id);
          $data['user_id'] = intval($this->params['user_id']);
          $data['admin_id'] = intval($admin_id);
          $data['type'] = intval($adminData['type']);
          $data['add_type'] = 1;
          $data['create_time'] = date('Y-m-d H:i:s');
          if($todaySetNum < $adminData['max_add_customer']){
            $res = $oAdminService->db_insert($data);
            if(!$res){
              $this->code = 1;
              $this->msg = '设置失败，请刷新后重试';
            }
          }else{
            $this->code = 1;
            $this->msg = '今天设置客户数已达到上限';
          }
        }else{
          $this->code = 1;
          $this->msg = '当前登录账号异常，请重新登录';
        }
      }else{
        $this->code = 1;
        $this->msg = '登录已失效，请重新登录';
      }
    }else{
      $this->code = 1;
      $this->msg = '缺少数据';
    }
    $this->boot();
  }
}